package cn.itcalvin.sncoal.cth.service.impl;

import cn.itcalvin.sncoal.cth.component.ConfigCacheUtils;
import cn.itcalvin.sncoal.cth.domain.ConfigSystem;
import cn.itcalvin.sncoal.cth.domain.ConfigSystemZb;
import cn.itcalvin.sncoal.cth.domain.ConfigSystemZbDetails;
import cn.itcalvin.sncoal.cth.domain.ConfigSystemZbDetailsExt;
import cn.itcalvin.sncoal.cth.mapper.main.ConfigSystemZbDetailsExtMapper;
import cn.itcalvin.sncoal.cth.mapper.main.ConfigSystemZbDetailsMapper;
import cn.itcalvin.sncoal.cth.mapper.main.ConfigSystemZbMapper;
import cn.itcalvin.sncoal.cth.pojo.req.config.ReqConfigSystemZbDetailsModify;
import cn.itcalvin.sncoal.cth.utils.JwtUtil;
import lombok.val;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Service;


import cn.itcalvin.sncoal.cth.mapper.main.ConfigSystemMapper;
import cn.itcalvin.sncoal.cth.service.ConfigSystemService;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ConfigSystemServiceImpl implements ConfigSystemService{

    @Resource
    private ConfigSystemMapper configSystemMapper;
    @Resource
    private ConfigSystemZbMapper configSystemZbMapper;
    @Resource
    private ConfigSystemZbDetailsMapper configSystemZbDetailsMapper;
    @Resource
    private ConfigSystemZbDetailsExtMapper configSystemZbDetailsExtMapper;


    /**
     * 考核的系统模块
     *
     * @return
     */
    @Override
    public List<ConfigSystem> listSystem() {
        Example example = Example.builder(ConfigSystem.class).where(Sqls.custom()
                .andEqualTo("isDelete", false)).build();
        return configSystemMapper.selectByExample(example);
    }

    /**
     * 考核的系统模块下的指标
     *
     * @return
     */
    @Override
    public Map<Long, List<ConfigSystemZb>> listSystemZb() {
        Example example = Example.builder(ConfigSystemZb.class).where(Sqls.custom()
                .andEqualTo("isDelete", false)).build();
        final val configSystemZbs = configSystemZbMapper.selectByExample(example);
        return configSystemZbs.stream().collect(Collectors.groupingBy(ConfigSystemZb::getSystemId));

    }

    /**
     * 考核的系统模块下的指标详情
     *
     * @return
     */
    @Override
    public Map<Long, List<ConfigSystemZbDetails>> listSystemZbDetails() {
        Example example = Example.builder(ConfigSystemZbDetails.class).where(Sqls.custom()
                .andEqualTo("isDelete", false)).build();
        final val configSystemZbs = configSystemZbDetailsMapper.selectByExample(example);
        return configSystemZbs.stream().collect(Collectors.groupingBy(ConfigSystemZbDetails::getSystemZbId));
    }


    /**
     * 指标扩展的加载
     *
     * @return
     */
    @Override
    public Map<Long, List<ConfigSystemZbDetailsExt>> listSystemZbDetailsExt() {
        Example example = Example.builder(ConfigSystemZbDetailsExt.class).where(Sqls.custom()
                .andEqualTo("isDelete", false)).build();
        final val configSystemZbs = configSystemZbDetailsExtMapper.selectByExample(example);
        return configSystemZbs.stream().collect(Collectors.groupingBy(ConfigSystemZbDetailsExt::getSystemZbDetailsId));
    }

    /**
     * 修改考核指标详情的溯源方法,如果不存在则新增
     *
     * @param reqConfigSystemZbDetailsModify
     * @return
     */
    @Override
    public Boolean modifyZbDetailsFunction(ReqConfigSystemZbDetailsModify reqConfigSystemZbDetailsModify) {
        final val userInfo = JwtUtil.getUserInfo();

        Example example = Example.builder(ConfigSystemZbDetailsExt.class).where(Sqls.custom()
                .andEqualTo("isDelete", false)
                .andEqualTo("mineId", userInfo.getBelongId())
                .andEqualTo("systemZbDetailsId", reqConfigSystemZbDetailsModify.getId())).build();
        final val configSystemZbs = configSystemZbDetailsExtMapper.selectOneByExample(example);
        if(configSystemZbs!=null){
            configSystemZbs.setZbDetailsFunction(reqConfigSystemZbDetailsModify.getZbDetailsFunction());
            Boolean success= configSystemZbDetailsExtMapper.updateByPrimaryKeySelective(configSystemZbs) > 0;
            if(success){
                ConfigCacheUtils.getInstance().modifyZbDetailsFunction(configSystemZbs);
            }
            return success;
        }
        return false;
    }
}
